import {defineStore} from 'pinia'
import {ref} from 'vue'
import {getUserInfoAPI} from '@/api/user.js'
import {mergeCartApi} from '@/api/cart.js'
import {useCartStore} from './cart.js'
export const useUserInfo=defineStore('userInfo',()=>{
    const userInfo=ref({})
    const cartStore=useCartStore()
    const getUserInfo=async ({account,password})=>{
        const res=await getUserInfoAPI({account,password})
        userInfo.value=res.data.result
        // 合并购物车
        await mergeCartApi(cartStore.cartList.map(item=>({skuId:item.skuId,selected:item.selected,count:item.count})))
        cartStore.getCartList()
       
    }
    const clearUserInfo=()=>{
        userInfo.value={}
        cartStore.clearCart()
    }
    return {userInfo,getUserInfo,clearUserInfo}
},{persist:true})